//
//  EditTimeController.m
//  QRMaster
//
//  Created by myApple on 15/7/31.
//  Copyright (c) 2015年 myApple. All rights reserved.
//

#import "EditTimeController.h"
#import "EditTimeVIew.h"
#import "TouchBtnColor.h"
#import "PromptInfoView.h"
#import "RequestNetWork.h"
#import "Reachability.h"
#import "StartPageController.h"
#import "IQKeyboardManager.h"
#import "EditTimeView.h"

@interface EditTimeController ()<EditTimeVIewDelegate>
{
    NSString * beginNewTime;
    NSString * endNewTime;
    //多语言
    UILabel * titleLabel;
    
    UIButton * sharebtn;
}

@end

@implementation EditTimeController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = RGBA(237, 238, 241, 1);
    [self prepareTitleView];
    [self prepareView];
    beginNewTime = _beginTime;
    endNewTime = _endTime;
}

-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    titleLabel.text = NSLocalizedString(@"setShareTime", nil);
    [IQKeyboardManager sharedManager].enableAutoToolbar = NO;
    sharebtn.hidden = NO;
}

-(void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    [IQKeyboardManager sharedManager].enableAutoToolbar = YES;
    sharebtn.hidden = YES;
}
-(void)setBeginTime:(NSString *)beginTime
{
    _beginTime = beginTime ;
    _beginTime = [_beginTime substringToIndex:16];
}

-(void)setEndTime:(NSString *)endTime
{
    _endTime = endTime;
    _endTime = [_endTime substringToIndex:16];
}


-(void)prepareTitleView
{
    float ScreenWidth = [UIScreen mainScreen].bounds.size.width;
    float x = ScreenWidth - 50;
    float y = 2;
    float width = 40;
    float height = 40;
    
    sharebtn = [[UIButton alloc]initWithFrame:CGRectMake(x, y, width, height)];
    [sharebtn addTarget:self action:@selector(sureClick) forControlEvents:UIControlEventTouchUpInside];
    [sharebtn setBackgroundImage:[TouchBtnColor imageWithColor:[UIColor grayColor] size:sharebtn.frame.size] forState:UIControlStateHighlighted];
    [self.navigationController.navigationBar addSubview:sharebtn];
    
    x = 10;
    y = 10;
    width = 20;
    height = 20;
    UIImageView * sureImgView = [[UIImageView alloc]initWithFrame:CGRectMake(x, y, width, height)];
    sureImgView.image = [UIImage imageNamed:@"ic_save"];
    [sharebtn addSubview:sureImgView];
    
}

-(void)prepareView
{
    float ScreenWidth = [UIScreen mainScreen].bounds.size.width;
    float ScreenHeight = [UIScreen mainScreen].bounds.size.height;
    float x = 0;
    float y = 0;
    float width = ScreenWidth;
    float height = ScreenHeight - y;
    EditTimeVIew * etv = [[EditTimeVIew alloc]initWithFrame:CGRectMake(x, y, width, height)];
    etv.delegate = self;
    etv.shareBeginTime = _beginTime;
    etv.shareEndTime = _endTime;
    [self.view addSubview:etv];
}

-(void)sureClick
{
    if (beginNewTime == nil) {
        [PromptInfoView showMessage:NSLocalizedString(@"beginTimeNil", nil) superView:self.view];
        return;
    }
    if (endNewTime == nil) {
        [PromptInfoView showMessage:NSLocalizedString(@"endTimeNil", nil) superView:self.view];
        return;
    }
    NSDateFormatter *dateFomatter = [[NSDateFormatter alloc] init];
    [dateFomatter setDateFormat:@"yyyy-MM-dd HH:mm"];
//    [dateFomatter setLocale:[[NSLocale alloc]initWithLocaleIdentifier:@"ch_CN"]];
    [dateFomatter setLocale:[NSLocale currentLocale]];

    NSDate * beginDate = [dateFomatter dateFromString:beginNewTime];
    NSDate * endDate = [dateFomatter dateFromString:endNewTime];
    
    NSComparisonResult result = [beginDate compare:endDate];
    if (result == NSOrderedDescending) {
        [PromptInfoView showMessage:NSLocalizedString(@"timeErrorChoose", nil) superView:self.view];
        return;
    }
    if ([self IsHaveNetwork]) {
        return;
    }
    [self startWaiting:nil color:nil addToView:self.view];
    NSUserDefaults * userInfo = [NSUserDefaults standardUserDefaults];
    NSMutableDictionary * postDict = [[NSMutableDictionary alloc]init];
    [postDict setValue:[userInfo objectForKey:@"uid"] forKey:@"id"];
    [postDict setValue:[userInfo objectForKey:@"token"] forKey:@"token"];
    [postDict setValue:_communityId forKey:@"communityId"];
    [postDict setValue:_roomId forKey:@"roomId"];
    [postDict setValue:endNewTime forKey:@"endTime"];
    [postDict setValue:beginNewTime forKey:@"startTime"];
    [postDict setValue:_shareId forKey:@"shareId"];
    __weak EditTimeController * weakSelf = self;
    RequestNetWork * rnk = [[RequestNetWork alloc]init];
    [rnk initWithPath:POST(EDITSHARE) params:postDict block:^(id downLoadData, id error) {
        [self stopWaiting];
        if ([downLoadData objectForKey:@"data"] != nil)
        {
            [weakSelf.navigationController popViewControllerAnimated:YES];
        }
        else if (error != nil)
        {
            [PromptInfoView showMessage:[error objectForKey:@"info"]];
        }

    }];
}

#pragma mark == delegate
-(void)EditTimeVIewSendTime:(NSString *)time typeTime:(BOOL)type
{
    if (type) {
        endNewTime = time;
    }else
    {
        beginNewTime = time;
    }
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
